<?php

namespace addons\Announce\api\modules\v1\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use common\enums\StatusEnum;
use api\controllers\OnAuthController;
use common\models\backend\Notify;
use common\models\backend\NotifyMember;
use common\helpers\ResultHelper;
use backend\modules\base\forms\NotifyAnnounceForm;

/**
 * 公告接口
 *
 *
 *
 * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass;
 * @author 古月 <21931118@qq.com>
 */
class DefaultController extends OnAuthController
{
    public $modelClass = Notify::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $authOptional = [];


    public function actionIndex()
    {
        Yii::$app->services->backendNotify->pullAnnounce(Yii::$app->user->identity->member_id, Yii::$app->user->identity->created_at);
        return new ActiveDataProvider([
            'query' => NotifyMember::find()
                ->andWhere(['>=', 'status', StatusEnum::DISABLED])
                ->andWhere(['type' => Notify::TYPE_ANNOUNCE])
                ->andFilterWhere(['member_id' => Yii::$app->user->identity->member_id??0])
                ->andFilterWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
                ->with(['notifySenderForMember'])
                ->orderBy('id desc')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }
    public function actionCreate()
    {
        /* @var $model \yii\db\ActiveRecord */
        $model = new $this->modelClass();
        $model->attributes = Yii::$app->request->post();
        $model->type = Notify::TYPE_ANNOUNCE;
        $model->sender_id = Yii::$app->user->identity->member_id;
        if (!$model->save()) {
            return ResultHelper::json(422, $this->getError($model));
        }
        return $model;
    }
    /**
     * 单个已读
     *
     * @return mixed
     */
    public function actionRead($notify_id)
    {
        // 设置为已读
        $member_id = Yii::$app->user->identity->member_id;
        return Yii::$app->services->backendNotify->read($member_id, [$notify_id]);
    }
    public function actionUnReadCount()
    {
        $member_id = Yii::$app->user->identity->member_id;

        return Yii::$app->services->backendNotify->unReadCount($member_id);
    }

    public function actionView($id)
    {
        if (empty($id) || empty(($model = NotifyMember::find()->where([
                'id' => $id,
                'status' => StatusEnum::ENABLED
            ])->one()))) {
            return $this->message('找不到该公告', $this->redirect(['index']), 'error');
        }

        // 设置公告为已读
        Yii::$app->services->backendNotify->read(Yii::$app->user->identity->member_id, [$model->notify_id]);

        return $model->notifySenderForMember;
    }
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = NotifyAnnounceForm::findOne($id)))) {
            $model = new NotifyAnnounceForm();

            return $model->loadDefaultValues();
        }

        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, [])) {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }


}